package com.panda.common.memcached;


import java.util.Properties;
import java.util.ResourceBundle;





public class MemcachedParam {

	private static Properties properites = new Properties();
	
	private static ResourceBundle rb;
	
	private int serverNum;
	
	/**
	 * 配置服务器信息
	 */
	private String[] servers;

	/**
	 * 配置服务器权重信息
	 */
	private Integer[] weights;

	/**
	 * 初始连接数
	 */
	private int initConn;

	/**
	 * 最小连接数
	 */
	private int minConn;

	/**
	 * 最大连接数
	 */
	private int maxConn;

	/**
	 * 最大处理时间,超时时间
	 */
	private long maxIdle;
	
	/**
	 * 主线程的睡眠时间
	 */
	private long maintSleep;
	
	/**
	 * Tcp的规则就是在发送一个包之前，本地机器会等待远程主机      
     * 对上一次发送的包的确认信息到来；这个方法就可以关闭套接字的缓存，      
     * 以至这个包准备好了就发； 
	 */
	private boolean nagle;
	
	/**
	 * 连接建立后对超时的控制
	 */
	private int socketTo;
	
	/**
	 * 连接建立时对超时的控制
	 */
	private int socketConnectTo;
	
	/**
	 * 是否开启压缩设置
	 */
	private boolean compressEnable;
	
	/**
	 * 超过指定大小（单位为K）的数据都会被压缩
	 */
	private long compressThreshold;
	
//	public static MemcachedParam mmcParam;
//	
//	/**
//	 *构造函数
//	 */
//	public MemcachedParam(){
//		
//	}
//	
//	/**
//	 * @获得本类实例
//	 * @return
//	 */
//	public static MemcachedParam getInstance(){
//		if (mmcParam == null)
//			mmcParam = new MemcachedParam();
//
//		return mmcParam;
//	}
	

	public boolean getCompressEnable() {
		return compressEnable;
	}

	public void setCompressEnable(boolean compressEnable) {
		this.compressEnable = compressEnable;
	}

	public long getCompressThreshold() {
		return compressThreshold;
	}

	public void setCompressThreshold(long compressThreshold) {
		this.compressThreshold = compressThreshold;
	}

	public int getInitConn() {
		return initConn;
	}

	public void setInitConn(int initConn) {
		this.initConn = initConn;
	}

	public long getMaintSleep() {
		return maintSleep;
	}

	public void setMaintSleep(long maintSleep) {
		this.maintSleep = maintSleep;
	}

	public int getMaxConn() {
		return maxConn;
	}

	public void setMaxConn(int maxConn) {
		this.maxConn = maxConn;
	}

	public long getMaxIdle() {
		return maxIdle;
	}

	public void setMaxIdle(long maxIdle) {
		this.maxIdle = maxIdle;
	}

	public int getMinConn() {
		return minConn;
	}

	public void setMinConn(int minConn) {
		this.minConn = minConn;
	}

	public boolean getNagle() {
		return nagle;
	}

	public void setNagle(boolean nagle) {
		this.nagle = nagle;
	}

	public String[] getServers() {
		return servers;
	}

	public void setServers(String[] servers) {
		this.servers = servers;
	}

	public int getSocketConnectTo() {
		return socketConnectTo;
	}

	public void setSocketConnectTo(int socketConnectTo) {
		this.socketConnectTo = socketConnectTo;
	}

	public int getSocketTo() {
		return socketTo;
	}

	public void setSocketTo(int socketTo) {
		this.socketTo = socketTo;
	}

	public Integer[] getWeights() {
		return weights;
	}

	public void setWeights(Integer[] weights) {
		this.weights = weights;
	}

	public MemcachedParam() throws Exception
	{
		this("memcached");

	}

	public MemcachedParam(String fileName) throws Exception
	{	
		rb = ResourceBundle.getBundle(fileName);
//		String base = this.getClass().getClassLoader().getResource("").toString();
//		//注意:一定要把file:/这个字符去掉,要FileInputStream不是找不到文件的
//		String filePath = base.substring(6,base.length())+fileName;
//		InputStream ips = new BufferedInputStream(new FileInputStream(filePath));
//		this.properites.load(ips);
		init();
	}
	
	public void init()
	{
		//初始连接数
		this.initConn = Integer.parseInt(rb.getString("initConn"));
		//System.out.println(initConn);
		//最小连接数
		this.minConn = Integer.parseInt(rb.getString("minConn"));
		//System.out.println(minConn);
		//最大连接数
		this.maxConn = Integer.parseInt(rb.getString("maxConn"));
		//System.out.println(maxConn);
		//最大处理时间,超时时间
		this.maxIdle = new Long(rb.getString("maxIdle"));
		//System.out.println(maxIdle);
		//主线程的睡眠时间
		this.maintSleep = new Long(rb.getString("maintSleep"));
		//System.out.println(maintSleep);
		//Tcp的规则就是在发送一个包之前，本地机器会等待远程主机      
		//对上一次发送的包的确认信息到来；这个方法就可以关闭套接字的缓存，      
		//以至这个包准备好了就发； 
		this.nagle = (rb.getString("nagle").equals("true"));
		//System.out.println(nagle);
		//连接建立后对超时的控制
		this.socketTo = Integer.parseInt(rb.getString("socketTo"));
		//System.out.println(socketTo);
		//连接建立时对超时的控制
		this.socketConnectTo = Integer.parseInt(rb.getString("socketConnectTo"));
		//System.out.println(socketConnectTo);
		//是否开启压缩设置
		this.compressEnable = (rb.getString("compressEnable").equals("true"));
		//System.out.println(compressEnable);
		//超过指定大小（单位为K）的数据都会被压缩
		this.compressThreshold = new Long(rb.getString("compressThreshold"));
		//System.out.println(compressThreshold);
		
		//服务器数量
		this.serverNum = Integer.parseInt(rb.getString("serverNum"));
		//System.out.println(serverNum);
		//配置服务器信息
		this.servers = new String[this.serverNum];
		this.weights = new Integer[this.serverNum];
		Integer j = 0;
		for (int i = 0; i < serverNum; i++)
		{
			String server = "server";
			server = server + (i+1);
			String weight = "weight";
			weight = weight + (i+1);
			servers[i] = (rb.getString(server));
			weights[i] = Integer.parseInt(rb.getString(weight));
		}
		
		

	}

	public int getServerNum() {
		return serverNum;
	}

	public void setServerNum(int serverNum) {
		this.serverNum = serverNum;
	}
	public static void main(String[] args) throws Exception
	{
		 //System.out.println("进入主函数");
		MemcachedParam conf = new MemcachedParam();
		String str = ""+conf.getServers()[0];
		System.out.println(str);
	}
}
